var app = getApp()
var utils = require('../../utils/utils.js')
Page( {
  data: {
    //地址页面选择过来的
    address: null,
    postAddress:null,
    postUser:null,
    postMobile:null,
  },
  //初始化地址
  onLoad: function(options) {
    //console.log("=====app.globalData.addressId====",app.globalData.addressId)
    this.setData({
      addressId :null,
    })
    var that = this
    var param = null
    if(app.globalData.addressId == null){
      param = {"openId":app.globalData.openid,"needAddress":true}
    }else{
      param = {"openId":app.globalData.openid,"needAddress":true,"addressId":app.globalData.addressId}
    }
    wx.request({
      url: utils.sysConst.getCartPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        if (res.data.code == 1){
          that.setData({
            cartProduct :res.data.data.productInfoList,
            discountAmount: res.data.data.discountAmount,
            orderAmount: res.data.data.orderAmount,
            canOrder: res.data.data.canOrder,
            buyNumber: res.data.data.buyNumber,
            errorMsg: res.data.data.errorMsg,
          })
          if (res.data.data.userAddressVo != null){
            app.globalData.addressId = res.data.data.userAddressVo.id
            that.setData({
              userAddressVo: res.data.data.userAddressVo,
              postAddress: res.data.data.userAddressVo.homeAddress,
              postUser:res.data.data.userAddressVo.addressee,
              postMobile:res.data.data.userAddressVo.mobile,
            })
          }
          console.log("=====app.globalData.addressId====",app.globalData.addressId)
        }else{
          that.setData({
            cartProduct :null,
            canOrder: false,
          })
        }
      }
    })
  },
  selectAddress:function(event){
    wx.navigateTo({
      url: '/pages/personal/userAddress/userAddress?type=select',
    })
  },
  order: function(event){
    var addressId  = app.globalData.addressId;
    if (addressId == null){
      wx.showToast({
        title:"请选择收货地址",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
     //设置提交按钮不可用
    this.setData({
      disabled: !this.data.disabled,
      loading: !this.data.loading
    })
    wx.showLoading({
      title: '处理中...',
    })
    var param = {"addressId":addressId,"openId":app.globalData.openid}
    wx.request({
      url: utils.sysConst.orderPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 1){
          wx.showToast({
            title: '下单成功',
            icon: 'success',
            duration: 2000
          })
          wx.navigateTo({
            url: '/pages/orderSuccess/orderSuccess?type=order',
          })
        }else{
          wx.showToast({
            title:res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    // 设置提交按钮可用
    this.setData({
      disabled: !this.data.disabled,
      loading: !this.data.loading
    })
  },
  //重新打开
  onShow: function () {

  },
})
